﻿using Core.IRepository;
using Core.IServices;
using Library.Cache;
using Library.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Core.Dtos;
using Microsoft.AspNet.Identity.EntityFramework;
using Core.Messages;
using Core.Domain;
using Core.Queries;
using Core.Aspects;

namespace Services
{
    [CacheName("User")]
    public class ManageUserService : BaseService, IManageUserService
    {
        private readonly IUserRepository repository;
        private readonly IUnitOfWork unitOfWork;
        private readonly IGenericReaderService<string, AppUser, UserDto, UserQuery> readerService;
        private readonly IGenericWriterService<string, AppUser, UserDto, UserQuery> writerService;

        public ManageUserService(INullUnitOfWork unitOfWork, IUserRepository userRepository, IGenericReaderService<string, AppUser, UserDto, UserQuery> readerService, IGenericWriterService<string, AppUser, UserDto, UserQuery> writerService)
        {
            this.repository = userRepository;
            this.unitOfWork = unitOfWork;
            this.readerService = readerService;
            this.writerService = writerService;
            readerService.Initialize(unitOfWork, userRepository);
            writerService.Initialize(unitOfWork, userRepository);
        }

        [HandleServiceError]
        public QueryResponse<RoleDto> GetRoles(string id)
        {
            QueryResponse<RoleDto> response = new QueryResponse<RoleDto>();
            
            IList<string> list = repository.GerRoles(id);
            response.PageNumber = 1;
            response.TotalItemCount = list.Count;
            response.List = list.Select<string, RoleDto>(role => new RoleDto() { Id = role, Name = role }).ToList();                      

            return response;
        }

        public Response AddToRole(string id, string role)
        {
            return Run(() =>
            {
                repository.AddToRole(id, role);                
            });   
        }

        public Response RemoveFromRole(string id, string role)
        {
            return Run(() => 
            { 
                repository.RemoveFromRole(id, role);                
            });            
        }

        [Cache]
        public UserDto FindByID(string id)
        {
            return readerService.FindByID(id);
        }

        public QueryResponse<UserDto> FindBy(Request<UserQuery> request)
        {
            return readerService.FindBy(request);
        }

        public Response<string> Add(Request<UserDto> dto)
        {
            return writerService.Add(dto);
        }

        [DeleteCache]
        public Response Edit(string id, Request<UserDto> dto)
        {
            return writerService.Edit(id, dto);
        }

        [DeleteCache]
        public Response Delete(string id)
        {
            return writerService.Delete(id);
        }
    }
}
